import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { CmtActivitySignupApi } from '#/api/cmt/activity/signup';

import { z } from '#/adapter/form';
import { getActivityInfoOptions } from '#/api/cmt/activity/info';
import { getBaseCommunityOptions } from '#/api/cmt/base/community';
import { getMerchantInfoOptions } from '#/api/cmt/base/merchantInfo';
import { getRangePickerDefaultProps } from '#/utils';

/** 审核状态选项 */
export const AUDIT_STATUS_OPTIONS = [
  { label: '待审核', value: 0 },
  { label: '通过', value: 1 },
  { label: '拒绝', value: 2 },
];

/** 签到状态选项 */
export const CHECKIN_STATUS_OPTIONS = [
  { label: '未签到', value: 0 },
  { label: '已签到', value: 1 },
];

/** 取消状态选项 */
export const CANCEL_STATUS_OPTIONS = [
  { label: '正常', value: 0 },
  { label: '已取消', value: 1 },
];

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'communityId',
      label: '社区',
      component: 'ApiSelect',
      formItemClass: 'col-span-1',
      componentProps: {
        api: () => getBaseCommunityOptions(),
        labelField: 'name',
        valueField: 'id',
        placeholder: '请选择社区',
      },
    },
    {
      fieldName: 'merchantId',
      label: '商户',
      component: 'ApiSelect',
      formItemClass: 'col-span-1',
      componentProps: {
        api: () => getMerchantInfoOptions(),
        labelField: 'name',
        valueField: 'id',
        placeholder: '请选择商户',
      },
    },
    {
      fieldName: 'activityId',
      label: '活动',
      rules: 'required',
      component: 'ApiSelect',
      formItemClass: 'col-span-1',
      componentProps: {
        api: () => getActivityInfoOptions(),
        labelField: 'title',
        valueField: 'id',
        placeholder: '请选择活动',
      },
    },
    {
      fieldName: 'userId',
      label: '用户ID',
      component: 'InputNumber',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入用户ID',
        controlsPosition: 'right',
        class: '!w-full',
      },
    },
    {
      fieldName: 'personId',
      label: '人员ID',
      component: 'InputNumber',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入人员ID',
        controlsPosition: 'right',
        class: '!w-full',
      },
    },
    {
      fieldName: 'signupTime',
      label: '报名时间',
      component: 'DatePicker',
      formItemClass: 'col-span-1',
      componentProps: {
        type: 'datetime',
        placeholder: '请选择报名时间',
        valueFormat: 'YYYY-MM-DD HH:mm:ss',
      },
    },
    {
      fieldName: 'auditStatus',
      label: '审核状态',
      component: 'Select',
      formItemClass: 'col-span-1',
      componentProps: {
        options: AUDIT_STATUS_OPTIONS,
        placeholder: '请选择审核状态',
      },
      defaultValue: 0,
    },
    {
      fieldName: 'auditTime',
      label: '审核时间',
      component: 'DatePicker',
      formItemClass: 'col-span-1',
      componentProps: {
        type: 'datetime',
        placeholder: '请选择审核时间',
        valueFormat: 'YYYY-MM-DD HH:mm:ss',
      },
    },
    {
      fieldName: 'auditBy',
      label: '审核人',
      component: 'InputNumber',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入审核人ID',
        controlsPosition: 'right',
        class: '!w-full',
      },
    },
    {
      fieldName: 'auditRemark',
      label: '审核备注',
      component: 'Input',
      formItemClass: 'col-span-2',
      componentProps: {
        type: 'textarea',
        rows: 2,
        placeholder: '请输入审核备注',
      },
    },
    {
      fieldName: 'checkinStatus',
      label: '签到状态',
      component: 'Select',
      formItemClass: 'col-span-1',
      componentProps: {
        options: CHECKIN_STATUS_OPTIONS,
        placeholder: '请选择签到状态',
      },
      defaultValue: 0,
    },
    {
      fieldName: 'checkinTime',
      label: '签到时间',
      component: 'DatePicker',
      formItemClass: 'col-span-1',
      componentProps: {
        type: 'datetime',
        placeholder: '请选择签到时间',
        valueFormat: 'YYYY-MM-DD HH:mm:ss',
      },
    },
    {
      fieldName: 'pointAwarded',
      label: '奖励积分',
      component: 'InputNumber',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入奖励积分',
        min: 0,
        controlsPosition: 'right',
        class: '!w-full',
      },
      defaultValue: 0,
    },
    {
      fieldName: 'experienceAwarded',
      label: '奖励经验',
      component: 'InputNumber',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入奖励经验',
        min: 0,
        controlsPosition: 'right',
        class: '!w-full',
      },
      defaultValue: 0,
    },
    {
      fieldName: 'cancelStatus',
      label: '取消状态',
      component: 'Select',
      formItemClass: 'col-span-1',
      componentProps: {
        options: CANCEL_STATUS_OPTIONS,
        placeholder: '请选择取消状态',
      },
      defaultValue: 0,
    },
    {
      fieldName: 'cancelTime',
      label: '取消时间',
      component: 'DatePicker',
      formItemClass: 'col-span-1',
      componentProps: {
        type: 'datetime',
        placeholder: '请选择取消时间',
        valueFormat: 'YYYY-MM-DD HH:mm:ss',
      },
    },
    {
      fieldName: 'cancelReason',
      label: '取消原因',
      component: 'Input',
      formItemClass: 'col-span-2',
      componentProps: {
        type: 'textarea',
        rows: 2,
        placeholder: '请输入取消原因',
      },
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'activityId',
      label: '活动',
      component: 'ApiSelect',
      componentProps: {
        api: () => getActivityInfoOptions(),
        labelField: 'title',
        valueField: 'id',
        clearable: true,
        placeholder: '请选择活动',
      },
    },
    {
      fieldName: 'userId',
      label: '用户ID',
      component: 'InputNumber',
      componentProps: {
        clearable: true,
        placeholder: '请输入用户ID',
        controlsPosition: 'right',
        class: '!w-full',
      },
    },
    {
      fieldName: 'auditStatus',
      label: '审核状态',
      component: 'Select',
      componentProps: {
        clearable: true,
        options: AUDIT_STATUS_OPTIONS,
        placeholder: '请选择审核状态',
      },
    },
    {
      fieldName: 'checkinStatus',
      label: '签到状态',
      component: 'Select',
      componentProps: {
        clearable: true,
        options: CHECKIN_STATUS_OPTIONS,
        placeholder: '请选择签到状态',
      },
    },
    {
      fieldName: 'cancelStatus',
      label: '取消状态',
      component: 'Select',
      componentProps: {
        clearable: true,
        options: CANCEL_STATUS_OPTIONS,
        placeholder: '请选择取消状态',
      },
    },
    {
      fieldName: 'createTime',
      label: '创建时间',
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        clearable: true,
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<CmtActivitySignupApi.ActivitySignup>['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      field: 'id',
      title: '报名ID',
      minWidth: 120,
    },
    {
      field: 'activityId',
      title: '活动ID',
      minWidth: 120,
    },
    {
      field: 'userId',
      title: '用户ID',
      minWidth: 120,
    },
    {
      field: 'personId',
      title: '人员ID',
      minWidth: 120,
    },
    {
      field: 'signupTime',
      title: '报名时间',
      minWidth: 150,
      formatter: 'formatDateTime',
    },
    {
      field: 'auditStatus',
      title: '审核状态',
      minWidth: 100,
      slots: { default: 'auditStatus' },
    },
    {
      field: 'auditTime',
      title: '审核时间',
      minWidth: 150,
      formatter: 'formatDateTime',
    },
    {
      field: 'checkinStatus',
      title: '签到状态',
      minWidth: 100,
      slots: { default: 'checkinStatus' },
    },
    {
      field: 'checkinTime',
      title: '签到时间',
      minWidth: 150,
      formatter: 'formatDateTime',
    },
    {
      field: 'pointAwarded',
      title: '奖励积分',
      minWidth: 100,
    },
    {
      field: 'experienceAwarded',
      title: '奖励经验',
      minWidth: 100,
    },
    {
      field: 'cancelStatus',
      title: '取消状态',
      minWidth: 100,
      slots: { default: 'cancelStatus' },
    },
    {
      field: 'cancelTime',
      title: '取消时间',
      minWidth: 150,
      formatter: 'formatDateTime',
    },
    {
      field: 'createTime',
      title: '创建时间',
      minWidth: 150,
      formatter: 'formatDateTime',
    },
    {
      title: '操作',
      width: 200,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
